

<template>
  <div>
    <basic-container>
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>版本推广管理</el-breadcrumb-item>
        <el-breadcrumb-item>渠道管理</el-breadcrumb-item>
      </el-breadcrumb>
    </basic-container>
    <basic-container>
      <avue-crud
        ref="crud"
        :data="data"
        v-model="form"
        :option="option"
        :page.sync="page"
        @on-load="getList"
        @refresh-change="refreshChange"
        @search-reset="from = {}"
        @search-change="searchChange"
        @row-update="rowUpdate"
        @row-save="rowSave"
      >
              <!-- @row-del="rowDel" -->
        <!-- 自定义按钮 -->
        <template slot="package" slot-scope="scope">
          <el-row>
            <el-button type="primary" size="small" @click="openPack(scope.row)"
              >打包</el-button
            >
          </el-row>
        </template>

        <template slot="download" slot-scope="scope">
          <el-row>
            <el-button type="success" size="small" @click="download(scope.row)"
              >下载</el-button
            >
          </el-row>
        </template>
      </avue-crud>
      <!-- 选择版本弹出框 -->
      <el-dialog
        title="请选择应用版本"
        :visible.sync="dialogVisible"
        width="30%"
        append-to-body
      >
        <el-form ref="form" label-width="150px">
          <el-form-item label="应用版本：">
            <el-select v-model="version_num" placeholder="请选择应用版本">
              <el-option
                v-for="(version, i) in versionList"
                :key="i"
                :label="version.app_version"
                :value="version.version_number"
              >
                <span style="float: left">{{ version.app_version }}</span>
                <span style="float: right; color: #8492a6; font-size: 13px">{{
                  version.version_number
                }}</span>
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="pack()">确 定</el-button>
        </span>
      </el-dialog>
    </basic-container>
  </div>
</template>

<script>
import { catUrl } from "@/config/env";
import { common } from "@/api/cat/httpRequest";
import { channelPack as baseUrl, selectVersion } from "@/api/cat/baseUrls";
export default {
  data() {
    return {
      version_num: "",
      versionList: [],
      selectChannel: {},
      dialogVisible: false,
      page: {
        total: 0,
        pageSize: 10,
      },
      type: true,
      data: [],
      params: {},
      form: {},
      option: {
        delBtn: true,
        editBtn: false,
        searchBtn: true,
        addBtn: true,
        column: [
          {
            label: "渠道号",
            prop: "channel_id",
            type: "input",
            search: true,
            addDisplay: false,
            editDisplay: false,
            addDisabled: true,
            editDisabled: true,
            readonly: true,
          },
          {
            label: "渠道名称",
            prop: "channel_info",
            type: "input",
            search: true,
            required: true,
          },
          {
            label: "渠道创建时间",
            prop: "create_time",
            type: "input",
            addDisplay: false,
            editDisplay: false,
            addDisabled: true,
            editDisabled: true,
            readonly: true,
          },
          {
            label: "打包",
            prop: "package",
            type: "input",
            required: true,
            slot: "true",
            addDisplay: false,
            editDisplay: false,
            addDisabled: true,
            editDisabled: true,
          },
          {
            label: "下载",
            prop: "download",
            type: "input",
            required: true,
            slot: true,
            addDisplay: false,
            editDisplay: false,
            addDisabled: true,
            editDisabled: true,
          },
        ],
      },
    };
  },
  methods: {
    /**获取列表 */
    async getList() {
      this.loading = true;
      //清空表格列表
      this.data = [];
      this.params.orderByList = "create_time&DESC";
      //发送请求获取数据
      let response = await common(
        `${baseUrl.pageAndParam}`,
        { ...this.params, ...this.defalut },
        this.page,
        "POST"
      );

      this.page = response.page;
      this.data = response.data;
      this.loading = false;
    },
    /**刷新 */
    refreshChange() {
      this.getList();
    },
    /**搜索 */
    searchChange(params, done) {
      params.channel_info = `${
        params.channel_info ? "%" + params.channel_info + "%" : ""
      }`;
      params.create_time = `${
        params.create_time ? "!<>!" + params.create_time : ""
      }`;
      this.params = params;
      this.page.currentPage = 1;
      this.getList();
      if (done) done();
    },
    /**保存 */
    async rowSave(row, done) {
      delete this.form["id"];
      let userInfo = this.$store.state.user.userInfo;
      this.form.edit_account = userInfo.username;
      let data = await common(baseUrl.save, this.form, null, "POST");
      if (data == null) {
        return;
      }
      this.getList();
      done();
    },
    /**修改 */
    async rowUpdate(row, index, done, loading) {
      let userInfo = this.$store.state.user.userInfo;
      row.edit_account = userInfo.username;
      let data = await common(baseUrl.update, row, null, "POST");
      if (data == null) {
        loading();
        return;
      }
      this.getList();
      done();
    },
    // 打包跳转
    download(scope) {
      this.$router.push({
        path: "/package/channeldownload",
        query: {
          channel_id: scope.channel_id,
          preRouter: this.$route.path,
        },
      });
    },
    /**删除 */
    rowDel(row) {
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        type: "warning",
      })
        .then(async () => {
          let response = await common(
            baseUrl.delete,
            { id: row.id },
            null,
            "GET"
          );
          // if (response == null) {
          //   return this.$message.error("操作失败");
          // }
        })
        .then(() => {
          this.getList();
        });
    },
    //打开发布窗口
    openPack(scope) {
      this.getVersionList();
      this.selectChannel = scope;
      this.version_num = "";
      this.dialogVisible = true;
    },
    async pack() {
      if (this.version_num == "") {
        this.$message.error("请选择应用版本");
        return;
      }
      let scope = this.selectChannel;
      let data = await common(
        baseUrl.channelPack,
        { channel_id: scope.channel_id, version_num: this.version_num },
        null,
        "POST"
      );
      this.dialogVisible = false;
    },
    async getVersionList() {
      let data = await common(selectVersion.selectVersion, null, null, "GET");
      if (data.code == 1) {
        this.versionList = data.data;
      }
    },
  },
};
</script>
<style>
</style>